package com.scisoft.scan.ws;

import android.nfc.Tag;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.depot.AreaBean;
import com.scisoft.scan.data.depot.DepotBarcodeBean;
import com.scisoft.scan.data.depot.DepotOperator;
import com.scisoft.scan.data.depot.DepotOrderBean;
import com.scisoft.scan.data.depot.DepotOrderDetailBean;
import com.scisoft.scan.data.depot.DepotOrderStatis;
import com.scisoft.scan.data.depot.DepotQueryBean;
import com.scisoft.scan.data.depot.WarehouseBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_Depot {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/DepotService?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://depot.scan.ws.dms.com/";

    //查询仓库信息
    public static List<WarehouseBean> DEPOT_01() throws Exception {
//        List<WarehouseBean> ret = new ArrayList<>();
//        WarehouseBean w0 = new WarehouseBean();
//        w0.depotId = "-1";
//        w0.depotNo = "-1";
//        w0.depotName = "全部仓库";
//        ret.add(w0);
//        WarehouseBean w1 = new WarehouseBean();
//        w1.depotId = "J01";
//        w1.depotNo = "J01";
//        w1.depotName = "J仓";
//        ret.add(w1);
//        WarehouseBean w2 = new WarehouseBean();
//        w2.depotId = "B01";
//        w2.depotNo = "B01";
//        w2.depotName = "B仓";
//        ret.add(w2);
//        return ret;

        //原有代码可以复用
        String arg0 = String.format("{\"flowNo\":\"DEPOT_01\",\"account\":\"%s\",\"scSequence\":\"%s\"}", SysParameter.user, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<WarehouseBean> gret = new ArrayList<WarehouseBean>();
        List<WarehouseBean> ret = new ArrayList<WarehouseBean>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            gret = gson.fromJson(jsonArray.toString(), new TypeToken<List<WarehouseBean>>() {
            }.getType());
            WarehouseBean bean = new WarehouseBean("0", "全部仓库", "全部仓库");
            ret.add(bean);
            for (int i = 0; i < gret.size(); i++) {
                WarehouseBean tmp = gret.get(i);
                ret.add(tmp);
            }
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询货位信息 depotId是仓库ID
    public static List<AreaBean> DEPOT_12(String depotId) throws Exception {
//        List<AreaBean> ret = new ArrayList<>();
//        AreaBean w0 = new AreaBean();
//        w0.areaId = "-1";
//        w0.areaName = "全部货位";
//        ret.add(w0);
//        AreaBean w1 = new AreaBean();
//        w1.areaId = "J01-1";
//        w1.areaName = "1区";
//        ret.add(w1);
//        AreaBean w2 = new AreaBean();
//        w2.areaId = "J01-2";
//        w2.areaName = "2仓";
//        ret.add(w2);
//        return ret;
        if (depotId == null || "0".equals(depotId) || "".equals(depotId)) {
            depotId = "@@";
        }
        String arg0 = String.format("{\"flowNo\":\"DEPOT_12\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, depotId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<AreaBean> gret = new ArrayList<AreaBean>();
        List<AreaBean> ret = new ArrayList<AreaBean>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            gret = gson.fromJson(jsonArray.toString(), new TypeToken<List<AreaBean>>() {
            }.getType());
            AreaBean bean = new AreaBean("0", "全部库区");
            ret.add(bean);
            for (int i = 0; i < gret.size(); i++) {
                AreaBean tmp = gret.get(i);
                ret.add(tmp);
            }
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //仓库订单统计(orderType 仓库 ：入仓出仓， depotId 仓库ID(全部仓库id 0),areaId 库区编号   orderTypeName 单据业务类型名称)
    public static DepotOrderStatis DEPOT_07(String orderType, String depotId, String areaId,String orderTypeName) throws Exception {

//        DepotOrderStatis ret = new DepotOrderStatis();
//        ret.allOrderCount = 10;
//        ret.undoOrderCount = 1;
//        ret.doneOrderCount = 8;
//        ret.doingOrderCount = 1;
//        return ret;

        //以下代码可以复用
        DepotOrderStatis ret = new DepotOrderStatis();
        DepotQueryBean conditions = new DepotQueryBean(orderType, depotId, areaId, 1, 1,orderTypeName);
        Gson gson = new Gson();
        String parm = gson.toJson(conditions);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_07\",\"jsonData\":%s,\"scSequence\":\"%s\",\"account\":\"%s\"}", parm, SysParameter.ScSequence, SysParameter.user);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(), new TypeToken<DepotOrderStatis>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        Log.d("bugcao-scan", ret.toString());
        return ret;
    }

    //查询仓库订单(orderStatus 1 全部2未处理3处理中4已完成 orderType 仓库 ：入仓出仓， depotId 仓库ID, areaId ：库区编号  orderTypeName 单据业务类型名称)
    public static List<DepotOrderBean> DEPOT_02(String orderStatus, String orderType, String depotId, String areaId,String orderTypeName) throws Exception {

//        List<DepotOrderBean> ret = new ArrayList<>();
//        DepotOrderBean orderBean = new DepotOrderBean();
//        orderBean.id = "1111111111111111";
//        orderBean.orderType = "1";
//        orderBean.orderTypeTmp = "入仓";
//        orderBean.no = "111111";
//        orderBean.orderNo = "22222";
//        orderBean.optime = "3333";
//        orderBean.orderInfo = "4444";
//        orderBean.orderState = "5555";
//        orderBean.operator = "张三";
//        orderBean.supplierName = "8888";
//        orderBean.warehouseName = "k仓";
//        orderBean.areaName = "A1区";
//        ret.add(orderBean);
//        return ret;

        //以下代码可以复用
        DepotQueryBean conditions = new DepotQueryBean(orderType, depotId, orderStatus, areaId,orderTypeName);
        Gson gson = new Gson();
        String parm = gson.toJson(conditions);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_02\",\"jsonData\":%s,\"scSequence\":\"%s\",\"account\":\"%s\"}", parm, SysParameter.ScSequence, SysParameter.user);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<DepotOrderBean> ret = new ArrayList<>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(), new TypeToken<List<DepotOrderBean>>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询一个仓库订单的详细信息（orderType 1出仓2入仓， id 订单id）
    public static List<DepotOrderDetailBean> DEPOT_03(String id, String orderType) throws Exception {

        /*List<DepotOrderDetailBean> ret = new ArrayList<>();
        DepotOrderDetailBean bean = new DepotOrderDetailBean();
        bean.dtlId="1";
        bean.partCode="3";
        bean.partNo="2";
        bean.partName="3";
        bean.supplierCode="3";
        bean.supplierName="3";
        bean.imgUrl="3";
        bean.position="3";
        bean.partCount="3";
        bean.notPickedCount="3";
        bean.pickedCount="3";
        bean.ifOk="1";
        bean.ifGyssm="0";
        bean.serialNo="3";
        ret.add(bean);
        DepotOrderDetailBean bean2 = new DepotOrderDetailBean();
        bean2.dtlId="2";
        bean2.partCode="2";
        bean2.partNo="3";
        bean2.partName="5";
        bean2.supplierCode="6";
        bean2.supplierName="7";
        bean2.imgUrl="8";
        bean2.position="9";
        bean2.partCount="10";
        bean2.notPickedCount="11";
        bean2.pickedCount="12";
        bean2.ifOk="0";
        bean2.ifGyssm="0";
        bean2.serialNo="16";
        ret.add(bean2);
        return ret;*/

        //以下代码可以复用
        DepotQueryBean conditions = new DepotQueryBean(1, id, orderType);
        Gson gson = new Gson();
        String parm = gson.toJson(conditions);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_03\",\"jsonData\":%s,\"scSequence\":\"%s\",\"account\":\"%s\"}", parm, SysParameter.ScSequence, SysParameter.user);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<DepotOrderDetailBean> ret = new ArrayList<>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(), new TypeToken<List<DepotOrderDetailBean>>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //获取当前订单的最后的操作员orderType 1出仓2入仓
    public static DepotOperator DEPOT_09(String id, String orderType) throws Exception {

//        DepotOperator ret = new DepotOperator();
//        ret.operatorName = pickId;
//        return ret;
        //以下代码可以复用
        String bizId = id + "," + orderType;
        String arg0 = String.format("{\"flowNo\":\"DEPOT_09\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}", SysParameter.ScSequence, SysParameter.user, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        DepotOperator ret = new DepotOperator();
        if (msg != null && msg.contains("成功")) {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<DepotOperator>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //强制更新当前订单的最后的操作员orderType 1出仓2入仓
    public static String DEPOT_10(String id, DepotOperator depotOperator, String orderType) throws Exception {

//        return "成功";
        //以下代码可以复用
        String bizId = id + "," + orderType + "," + depotOperator.operatorName;
        String arg0 = String.format("{\"flowNo\":\"DEPOT_10\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}", SysParameter.ScSequence, SysParameter.user, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //单个明细 完成（orderType 1出仓2入仓， dtlId 明细ID）
    public static String DEPOT_04(String orderType, String dtlId) throws Exception {

//        return  "成功";
        //以下代码可以复用
        DepotOrderDetailBean bean = new DepotOrderDetailBean(dtlId);
        Gson gson = new Gson();
        String parm = gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_04\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence, orderType);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //上传扫描配件的唯一码（orderType 1出仓2入仓， dtlId 明细ID,serialNo 唯一码）
    public static String DEPOT_08(String orderId, String orderType, String dtlId, String serialNo) throws Exception {

//        return "成功";
        //以下代码可以复用
        DepotBarcodeBean bean = new DepotBarcodeBean();
        bean.orderId = orderId;
        bean.serialNo = serialNo;
        bean.orderDtlId = dtlId;
        Gson gson = new Gson();
        String parm = gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_08\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence, orderType);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //删除扫描配件的唯一码（orderType 1出仓2入仓， dtlId 明细ID,serialNo 唯一码）
    public static String DEPOT_11(String orderId, String orderType, String dtlId, String serialNo, String supplierCode) throws Exception {

        //以下代码可以复用
        Gson gson = new Gson();
        DepotOrderDetailBean bean = new DepotOrderDetailBean();
        bean.dtlId = dtlId;
        bean.serialNo = serialNo;
        String parm = gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_11\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence, orderType);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return ret;

    }

    //单个明细处理取消 完成（orderType 1出仓2入仓， dtlId 明细ID）
    public static String DEPOT_05(String orderType, String dtlId) throws Exception {

//        return "成功";
        //以下代码可以复用
        DepotOrderDetailBean bean = new DepotOrderDetailBean(dtlId);
        Gson gson = new Gson();
        String parm = gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_05\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence, orderType);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //所有完成 处理完成 （orderType 1出仓2入仓， DepotBarcodeBean 扫码信息）
    public static String DEPOT_06(String orderType, DepotBarcodeBean bean) throws Exception {

//        return "成功";
        //以下代码可以复用
        Gson gson = new Gson();
        String parm = gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_06\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence, orderType);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //录入包装区域
    public static String DEPOT_13(String orderId, String orderType, String packArea) throws Exception {
//        return "成功";
        if (packArea == null || "".equals(packArea)) {
            packArea = "@@";
        }
        String bizId = orderId + "," + orderType + "," + packArea;
        String arg0 = String.format("{\"flowNo\":\"DEPOT_13\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}", SysParameter.ScSequence, SysParameter.user, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //根据配件代码查询订单
    public static DepotOrderBean DEPOT_14(String partCode) throws Exception {
//        DepotOrderBean orderBean = new DepotOrderBean();
//        orderBean.id = "1111111111111111";
//        orderBean.orderType = "1";
//        orderBean.no = "111111";
//        orderBean.orderNo = "22222";
//        orderBean.optime = "3333";
//        orderBean.orderInfo = "4444";
//        orderBean.orderState = "5555";
//        orderBean.operator = "张三";
//        orderBean.supplierName = "8888";
//        orderBean.areaName = "A1区";
//        return orderBean;
        //以下代码可以复用
        DepotOrderBean conditions = new DepotOrderBean(partCode);
        Gson gson = new Gson();
        String parm = gson.toJson(conditions);
        String arg0 = String.format("{\"flowNo\":\"DEPOT_14\",\"jsonData\":%s,\"scSequence\":\"%s\",\"account\":\"%s\"}", parm, SysParameter.ScSequence, SysParameter.user);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        DepotOrderBean ret = new DepotOrderBean();
        if (msg != null && msg.contains("成功")) {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(), new TypeToken<DepotOrderBean>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //获取入库订单字典列表
    public static List<String> DEPOT_15()throws Exception{
//        List<String> ret = new ArrayList<>();
//        ret.add("全部");
//        ret.add("加急订单");
//        return ret;
        //原有代码可以复用
        String arg0 = String.format("{\"flowNo\":\"DEPOT_15\",\"account\":\"%s\",\"scSequence\":\"%s\"}", SysParameter.user, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<String> gret = new ArrayList<String>();
        List<String> ret = new ArrayList<String>();
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            gret = gson.fromJson(jsonArray.toString(), new TypeToken<List<String>>() {
            }.getType());
            String bean ="全部";
            ret.add(bean);
            for (int i = 0; i < gret.size(); i++) {
                String tmp = gret.get(i);
                ret.add(tmp);
            }
        } else {
            throw new Exception(msg);
        }
        return ret;

    }

    public static SoapObject CallWebservice(String arg0) throws Exception {
        SoapObject result = null;
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用
        // 获取返回的数据
        if (envelope.bodyIn instanceof SoapFault) {
            String msg = ((SoapFault) envelope.bodyIn).faultstring;
            throw new Exception(msg);
        } else {
            result = (SoapObject) envelope.bodyIn;
        }
        result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
